﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Models.Report;
using S4Store.Domain;
using S4Store.Service;
using S4Store.Domain.AfterSaleKpi;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using S4Store.Models;
using S4Store.Models.AfterSaleReport;

namespace S4Store.Controllers
{
    public class AfterSaleDecoratingReportController : ReportBaseController
    {
        //
        // GET: /AfterSaleDecoratingReport/
        public AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }
        public EmployeeService EmployeeService { get; set; }

        public ActionResult Index()
        {
            AfterSaleDecoratingReportModel model = new AfterSaleDecoratingReportModel();
            return View();
        }

        public ActionResult CarDecoratingSaleIndex()
        {
            AfterSaleDecoratingReportModel model = new AfterSaleDecoratingReportModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(AfterSaleCarKPIService);

                model.MonthConfig = AfterSaleCarKPIService.GetAfterSaleCarDecoratingMonthConfigs(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed).ToList();

                model.DayData = AfterSaleCarKPIService.GetAfterSaleCarDecoratingDayInputs(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed).ToList();

                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "CarDecoratingSaleIndex";
                model.BarModel.Controller = "AfterSaleDecoratingReport";
                model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='各车型精品销售状况KPI历史数据查询' href='" + Url.Action("CarDecoratingSaleHistory") + "' ><span>历史数据查询</span></a>";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();
            }
            return View(model);
        }

        public ActionResult CarBrandDecoratingSaleKPI(int brandid=0)
        {
            if (brandid == 0)
            {
                brandid = SearchCarBrandId ?? 0;
            }

            AfterSaleDecoratingReportModel model = new AfterSaleDecoratingReportModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(AfterSaleCarKPIService);

                model.MonthConfig = AfterSaleCarKPIService.GetAfterSaleCarDecoratingMonthConfigs(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed && o.CarBrandId == brandid).ToList();

                model.DayData = AfterSaleCarKPIService.GetAfterSaleCarDecoratingDayInputs(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed && o.CarBrandId==brandid).ToList();

                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "CarBrandDecoratingSaleKPI";
                model.BarModel.Controller = "AfterSaleDecoratingReport";
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='历史数据查询' href='" + Url.Action("CarOneBrandDecoratingSaleHistory", new { brandid=brandid}) + "' ><span>历史数据查询</span></a> <a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt()  ,referdesc="售后管理>用品销售动态>车型查询" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a>";
				}else
		{
			  model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='历史数据查询' href='" + Url.Action("CarOneBrandDecoratingSaleHistory", new { brandid=brandid}) + "' ><span>历史数据查询</span></a> ";
			}
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                model.BarModel.SearchCarBrandId = brandid;
                t.Commit();
            }
            return View(model);
        }

        public ActionResult CarOneBrandDecoratingSaleHistory(int brandid = 0)
        {
            AfterSaleDecoratingReportModel model = new AfterSaleDecoratingReportModel();
            if (brandid == 0)
                brandid = SearchCarBrandId ?? 0;
            model.BrandId = brandid;

            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using (Transtion t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                model.DayData = AfterSaleCarKPIService.GetAfterSaleCarDecoratingDayDataQuery().Where(o => o.AddTime <= end && o.AddTime >= start && o.IsConfirmed && o.CarBrandId==brandid).ToList();
                model.MonthConfig = AfterSaleCarKPIService.AfterSaleCarDecoratingMonthConfigQuery().Where(o => o.AddTime <= end && o.AddTime >= start && o.IsConfirmed && o.CarBrandId == brandid).ToList();
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "CarOneBrandDecoratingSaleHistory";
            model.BarModel.Controller = "AfterSaleDecoratingReport";
            model.BarModel.Start = start;
            model.BarModel.End = end;
            model.BarModel.SearchCarBrandId = brandid;
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        
        }

        public ActionResult CarDecoratingSaleHistory()
        {
            return CarDecoratingSaleHistory(DateTime.Now.AddMonths(-6), DateTime.Now);
        }

        [HttpPost]
        public ActionResult CarDecoratingSaleHistory(DateTime start, DateTime end)
        {
            AfterSaleDecoratingReportModel model = new AfterSaleDecoratingReportModel();
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using(Transtion t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                model.DayData = AfterSaleCarKPIService.GetAfterSaleCarDecoratingDayDataQuery().Where(o => o.AddTime <= end && o.AddTime >= start && o.IsConfirmed).ToList();
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "CarDecoratingSaleHistory";
            model.BarModel.Controller = "AfterSaleDecoratingReport";
            //model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.Start = start;
            model.BarModel.End = end;
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        }


        public ActionResult CarDecoratingSellerIndex()
        {
            AfterSaleSellerDecoratingReportModel model = new AfterSaleSellerDecoratingReportModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);

                model.Employees = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).ToList();
                int[] employs = model.Employees.Select(o => o.Id).ToArray();
                model.MonthConfig = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargets(Month.Year, Month.Month,employs).Where(o => o.IsConfirmed).ToList();

                model.DayData = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputs(Month.Year, Month.Month,employs).Where(o => o.IsConfirmed).ToList();

                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "CarDecoratingSellerIndex";
                model.BarModel.Controller = "AfterSaleDecoratingReport";
                //model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='各车型精品销售状况KPI历史数据查询' href='" + Url.Action("CarDecoratingSaleHistory") + "' ><span>历史数据查询</span></a>";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();
            }
            return View(model);
        }

        public ActionResult CarSellerDecoratingSaleKPI(int sellerid=0)
        {
            AfterSaleSellerDecoratingReportModel model = new AfterSaleSellerDecoratingReportModel();
            if (sellerid == 0)
                sellerid = SearchSellerId ?? 0;

            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using (Transtion t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);

                model.TimeRate = new Models.Chart.Pie();
                model.TimeRate.Width = 450;
                model.TimeRate.Height = 300;
                model.TimeRate.IsRate = true;
                model.TimeRate.YAxis = "%";
                model.TimeRate.Name = "时间进度";
                model.TimeRate.Data.Add("时间进度", 时间进度);
                model.TimeRate.Data.Add("剩余", 1 - 时间进度);

                model.Employees = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).ToList();
                int[] employs = model.Employees.Select(o => o.Id).ToArray();
                model.MonthConfig = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargets(Month.Year, Month.Month, employs).Where(o => o.IsConfirmed && o.EmployeeId==sellerid).ToList();
                model.DayData = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputs(Month.Year, Month.Month, employs).Where(o => o.IsConfirmed && o.EmployeeId==sellerid).ToList();

                ViewBag.时间进度 = 时间进度;
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "CarSellerDecoratingSaleKPI";
            model.BarModel.Controller = "AfterSaleDecoratingReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            //model.BarModel.Start = start;
            //model.BarModel.End = end;
            model.BarModel.SearchSellerId = sellerid;
			if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
            model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>用品销售动态>人员查询" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a> <a target='navtab' class='historyrecord' title='用品销售历史数据对比' href='" + Url.Action("CarSASellerHistory", new { sellerid = sellerid }) + "' ><span>历史数据查询</span></a>";
			}else
		{
			  model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='用品销售历史数据对比' href='" + Url.Action("CarSASellerHistory", new { sellerid = sellerid }) + "' ><span>历史数据查询</span></a>";
			}
            //model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
            
        }

        public ActionResult CarSASellerHistory(int sellerid = 0)
        {

            AfterSaleSellerDecoratingReportModel model = new AfterSaleSellerDecoratingReportModel();
            if (sellerid == 0)
                sellerid = SearchSellerId ?? 0;

            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using (Transtion t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                model.DayData = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputQuery().Where(o => o.AddTime <= end && o.AddTime >= start && o.IsConfirmed && o.EmployeeId == sellerid).ToList();
                model.MonthConfig = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargetQuery().Where(o => o.AddTime <= end && o.AddTime >= start && o.IsConfirmed && o.EmployeeId == sellerid).ToList();
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "CarSASellerHistory";
            model.BarModel.Controller = "AfterSaleDecoratingReport";
            model.BarModel.Start = start;
            model.BarModel.End = end;
            model.BarModel.SearchSellerId = sellerid;
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        }
    }
}
